/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.ClinicRecord;
import com.jims.master.modules.doctor.entity.HisDiagnosis;
import com.jims.master.modules.doctor.service.ClinicRecordService;
import com.jims.master.modules.doctor.service.HisDiagnosisService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Controller
 * @author 陈涛
 * @version 2017-03-16
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/clinicRecord")
public class ClinicRecordController extends BaseController {

	@Autowired
	private ClinicRecordService clinicRecordService;
	@Autowired
	private HisDiagnosisService hisDiagnosisService;
	
	@ModelAttribute
	public ClinicRecord get(@RequestParam(required=false) String id) {
		ClinicRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = clinicRecordService.get(id);
		}
		if (entity == null){
			entity = new ClinicRecord();
		}
		return entity;
	}

	/**
	 * 病人病例页面
	 * @param clinicRecord
	 * @param request
	 * @param response
	 * @param model
	 * @author 陈涛
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ClinicRecord clinicRecord,HisDiagnosis hisDiagnosis, HttpServletRequest request,String patientId, String clinicId ,HttpServletResponse response, Model model) {
		clinicRecord = clinicRecordService.getRecordByClinicId(clinicId);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String time = (df.format(new Date()));
		List<HisDiagnosis> list = hisDiagnosisService.getByClinicId(clinicId,"");
		model.addAttribute("hisDiagnosis", hisDiagnosis);
		UserUtils userUtils = new UserUtils();
		model.addAttribute("creator",userUtils.getUser().getId());
		model.addAttribute("billingDept",userUtils.getUser().getOrgDept().getId());
		model.addAttribute("userName", userUtils.getUser().getName());
		model.addAttribute("time", time);
		if(clinicRecord == null){
			ClinicRecord clinicRecord1  = new ClinicRecord();
			clinicRecord1.setClinicId(clinicId);
			clinicRecord1.setPatientId(patientId);
			model.addAttribute("clinicRecord", clinicRecord1);
		} else {
			model.addAttribute("clinicRecord", clinicRecord);
		}
		model.addAttribute("list", list);
		return "modules/doctor/clinicRecordIndex";
	}

	/**
	 * 历史病历列表
	 * @param patientId
	 * @param clinicId
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "historyRecord")
	public String historyRecord(String patientId,String clinicId ,Model model){
		ClinicRecord clinicRecord = new ClinicRecord();
		clinicRecord.setPatientId(patientId);
		clinicRecord.setClinicId(clinicId);
		clinicRecord.updateOrgId();
		List<ClinicRecord> clinicRecordList = clinicRecordService.findList(clinicRecord);
		model.addAttribute("clinicRecordList", clinicRecordList);
		model.addAttribute("clinicRecord", clinicRecord);
		return "modules/doctor/historyRecord";
	}

	/**
	 * 历史病历明细列表
	 * @param patientId
	 * @param clinicId
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "historyItemRecord")
	public String historyItemRecord(String id,String patientId,String clinicId ,Model model){
		ClinicRecord clinicRecord = new ClinicRecord();
		clinicRecord.setPatientId(patientId);
		clinicRecord.updateOrgId();
		ClinicRecord clinicItemRecord = clinicRecordService.get(id);
		List<HisDiagnosis> hisDiagnosisList = hisDiagnosisService.getByClinicId("",patientId);
		model.addAttribute("clinicItemRecord", clinicItemRecord);
		model.addAttribute("hisDiagnosisList", hisDiagnosisList);
		model.addAttribute("clinicRecord", clinicRecord);
		return "modules/doctor/historyItemRecord";
	}


	@RequiresPermissions("user")
	@RequestMapping(value = {"clinicRecordIndex", ""})
	public String clinicRecordIndex( String clinicId ,Model model) {
		List<HisDiagnosis> list = hisDiagnosisService.getByClinicId(clinicId,"");
		model.addAttribute("list", list);
		return "modules/doctor/clinicRecordTable";
	}

	/**
	 * 病人病例打印
	 * @param
	 * @return
	 * @author fd
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"recordPrint", ""})
	@ResponseBody
	public StringData recordPrint(String id,String clinicId,String patientId,String type){
		StringData stringData = new StringData();
		String printPath =clinicRecordService.printRegisit(id,clinicId,patientId,type);
		stringData.setCode("success");
		stringData.setData(printPath);
		return stringData;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ClinicRecord clinicRecord, Model model) {
		model.addAttribute("clinicRecord", clinicRecord);
		return "modules/doctor/clinicRecordForm";
	}

	/**
	 * 病人病例保存
	 * @param clinicRecord
	 * @param
	 * @return
	 * @author chentao
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ClinicRecord clinicRecord, String patientIdVal,String clinicId) {
		String orgId = UserUtils.getOrgId();
		clinicRecord.setOrgId(orgId);
		clinicRecord.setClinicId(clinicId);
		clinicRecord.setPatientId(patientIdVal);
	    StringData data=new StringData();
		int i=clinicRecordService.saveClinicRecord(clinicRecord);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}

	/**
	 * 病人病例保存
	 * @param clinicRecord
	 * @param
	 * @return
	 * @author chentao
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "saveRecord")
	@ResponseBody
	public StringData saveRecord(ClinicRecord clinicRecord, String patientIdVal,String clinicId) {
		String orgId = UserUtils.getOrgId();
		clinicRecord.setOrgId(orgId);
		clinicRecord.setClinicId(clinicId);
		clinicRecord.setPatientId(patientIdVal);
		StringData data=new StringData();
		int i=clinicRecordService.saveRecord(clinicRecord);
		if(i>0){
			data.setCode("success");
			data.setData("保存成功");
		}else{
			data.setCode("error");
			data.setData("保存失败");
		}
		return data;
	}


	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ClinicRecord clinicRecord) {
	    StringData data=new StringData();
		int i=clinicRecordService.delete(clinicRecord);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}